﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace clq_function
{
    class type_conversion
    {
        /// <summary>
        /// 读取二进制文件
        /// clq
        /// 2012/12/18
        /// </summary>
        /// <param name="filePath"></param>
        public void readBinaryFile(string filePath)
        {
            FileStream fs;
            fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            //读取整型int32
            byte[] b = br.ReadBytes(4);
            int num = BitConverter.ToInt32(b, 0);

            //读取double
            b = br.ReadBytes(8);
            double x = BitConverter.ToDouble(b, 0);

            //读取char[40]
            //char最后为'\0',可以直接用string来读
            b = br.ReadBytes(40);
            string nameStr = Encoding.ASCII.GetString(b);
            char[] ch = nameStr.ToCharArray();

            br.Close();
            fs.Close();
        }

        /// <summary>
        /// 写二进制文件
        /// clq
        /// 2012/12/18
        /// </summary>
        /// <param name="filePath"></param>
        public void writePointBinaryFile(string filePath)
        {
            FileStream fs;
            try
            {
                fs = new FileStream(filePath, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);

                //写入double
                double x = 120.1234;
                double y = 30.1234;
                bw.Write(x);
                bw.Write(y);

                //写入char
                char[] Name = "PI01".ToCharArray();
                bw.Write(Name);
                bw.Write('\0');
                //bw.Seek(-(40 - p.Name.Length - 1), SeekOrigin.Current);
                //byte[] b = new byte[40 - p.Name.Length - 1];
                //bw.Write(b);
                //char c = new char();

                //写入指定的二进制数
                string str = "11001100";
                byte b = Convert.ToByte(str, 2);
                for (int i = 0; i < 40 - Name.Length - 1; i++)
                {
                    bw.Write(b);
                }

                //写入整型
                int PointID = 1000;
                bw.Write(PointID);

                bw.Close();
                fs.Close();
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        /// <summary>
        /// 打印日志 
        /// 写txt文件
        /// clq
        /// 2012/12/18
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="message"></param>
        public void createLog(string filePath, string message)
        {
            //如果路径为空，则在改程序运行目录中
            if (filePath.Length == 0 || filePath == null)
            {
                filePath = "log.txt";
            }

            if (!File.Exists(filePath))
            {
                FileStream f = File.Create(filePath);
                f.Close();
                f.Dispose();
            }
            System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(DateTime.Now.ToString() + '\t' + message);
            sw.Flush();
            sw.Close();
            fs.Close();
            sw.Dispose();
            fs.Dispose();
        }

        /// <summary>
        /// 读取文本
        /// clq
        /// 2012/12/18
        /// </summary>
        /// <param name="filePath"></param>
        public void readTxt(string filePath)
        {
            if (!File.Exists(filePath))
            {
                Console.WriteLine("file does not exist!");
                return;
            }

            StreamReader sr = new StreamReader(filePath);
            //StreamReader sr = new StreamReader(filePath,Encoding.GetEncoding("UTF8"));

            string str = "";
            while (!sr.EndOfStream)
            {
                str = sr.ReadLine();
            }

        }
    }


}
